package org.ccheng.gui.index;

import cn.hutool.cron.CronUtil;
import de.felixroske.jfxsupport.AbstractFxmlView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.ccheng.MainApplication;
import org.ccheng.gui.info.InfoView;
import org.ccheng.gui.login.LoginView;
import org.ccheng.xztx.scheduler.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
public class IndexCtrl implements Initializable, EventHandler<WindowEvent> {

    @FXML
    private TabPane tabPane;
    @Autowired
    private InfoView infoView;

    public IndexCtrl() {
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Stage stage = MainApplication.getStage();
        stage.setOnCloseRequest(this);
        stage.setOnHidden(this);

        // 启动定时任务
        Scheduler.start();

        tabPane.getTabs().add(getTab("信息", infoView));
    }

    private Tab getTab(String text, AbstractFxmlView view) {
        Tab tab = new Tab(text);
        tab.setClosable(false);
        tab.setContent(view.getView());
        return tab;
    }


    @Override
    public void handle(WindowEvent event) {
        CronUtil.stop();
        System.exit(0);
    }
}
